package edu.gatech.cs2340.teamrocket.model.storage;

import java.text.DecimalFormat;
import edu.gatech.cs2340.teamrocket.control.exception.InvalidTransactionException;
import edu.gatech.cs2340.teamrocket.model.item.Item;

/**
 * The player's inventory.
 *
 * @author Joe Tacheron
 */
public class Backpack extends BasicStorage {

    private static final int MAX_CAPACITY = 800;

    private static final String NAME = "Player";
    private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("0.00");

    private double weight;

    public Backpack(){
        super(NAME);
    }

    /**
     * Get the amount of free weight that can hold Items.
     *
     * @return weight limit minus used weight
     */
    public double getAvailableWeight(){
        return MAX_CAPACITY - weight;
    }

    public void deductMoney(int amount){
        money -= amount;
        money = Math.max(0, money);
    }

    @Override
    public void validate(){
        super.validate();
        if(weight > MAX_CAPACITY)
            throw new InvalidTransactionException("<html>Oops! It looks like you don't have " +
            		"enough space to carry all these item(s)");
    }

    @Override
    @Deprecated
    public void become(BasicStorage pi){
        super.become(pi);
        this.weight = ((Backpack)pi).weight;
    }

    @Override
    @Deprecated
    public Backpack clone(){
        BasicStorage bsClone = super.clone();
        Backpack retval = new Backpack();
        retval.weight = this.weight;
        retval.money = bsClone.money;
        retval.items = bsClone.items;
        return retval;
    }

    @Override
    public void removeItem(Item i, int quantity) {
        super.removeItem(i, quantity);
        weight -= i.getWeight() * quantity;
    }

    @Override
    public void addItem(Item i, int quantity) {
        super.addItem(i, quantity);
        weight += i.getWeight() * quantity;
    }

    @Override
    public void addMoney(Item i, int quantity){
        money += i.getPrice() * quantity * ShopInventory.BUYBACK_RATIO;
    }

    @Override
    public boolean canBuyItem(Item i, int quantity) {
        return super.canBuyItem(i, quantity) && getAvailableWeight() >= i.getWeight() * quantity;
    }

    @Override
    protected String htmlBody(){
        StringBuilder retval = new StringBuilder();
        retval.append("Available Weight: ");
        retval.append("<p>");
        retval.append(DECIMAL_FORMAT.format(getAvailableWeight()));
        retval.append("<p>");
    	retval.append(super.htmlBody());
        return retval.toString();
    }

}
